package cn.itcast.bos.web.action.take_delivery;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.take_delivery.Promotion;
import cn.itcast.bos.service.take_delivery.PromotionService;
import cn.itcast.bos.web.action.common.BaseAction;

@Controller
@Namespace("/")
@ParentPackage("struts-default")
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class PromotionAction extends BaseAction<Promotion> {

	private File subjectImg;
	private String subjectImgFileName;

	public void setSubjectImg(File subjectImg) {
		this.subjectImg = subjectImg;
	}

	public void setSubjectImgFileName(String subjectImgFileName) {
		this.subjectImgFileName = subjectImgFileName;
	}

	@Autowired
	private PromotionService promotionService;
	
	// 添加
	@Action(value = "promotion_add", results = {
			@Result(name = "success", type = "redirect", location = "./pages/take_delivery/promotion.html") })
	public String promotionAdd() {
		// 文件重命名
		String uuid = UUID.randomUUID().toString().replace("-", "");
		String fileName = uuid + subjectImgFileName.substring(subjectImgFileName.lastIndexOf('.'));
		// 获得绝对路径以保存文件
		String rootPath = ServletActionContext.getServletContext().getRealPath("/") + "upload/";
		// 获得url以前台页面展示
		String rootUrl = ServletActionContext.getServletContext().getContextPath() + "/upload/";
		// 目录分离算法
		int h1 = fileName.hashCode();
		int d1 = h1 & 0xf;
		int h2 = h1 >>> 4;
		int d2 = h2 & 0xf;
		String path = d1 + "/" + d2 + "/";
		model.setTitleImg(rootUrl + path + fileName);
		// 保存至数据库
		promotionService.save(model);
		// 判断目录是否存在
		File dir = new File(rootPath, path);
		if (!dir.exists()) {
			dir.mkdirs();
		}
		File destFile = new File(dir, fileName);
		try {
			// 复制文件到硬盘
			FileUtils.copyFile(subjectImg, destFile);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}

	// 分页展示
	@Action(value = "promotion_showPage")
	public void showPage() {
		Specification<Promotion> specification = new Specification<Promotion>() {
			@Override
			public Predicate toPredicate(Root<Promotion> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				// TODO Auto-generated method stub
				return null;
			}
		};
		Pageable pageable = new PageRequest(page, rows);
		Page<Promotion> pageData = promotionService.findAll(specification, pageable);
		writeJson(pageData);
	}
}
